import React, { Component } from 'react'
import { getAesList } from '../../api'
import { Button } from 'antd'
//import { decrypt } from '../../utils/crypto.js'
import PDF from 'react-pdf-js';
//import { Document, Page } from 'react-pdf';
import {file} from 'electron'
//const fs = require('fs')

/**
 * 商品管理 路由
 */
class Product extends Component {
    constructor(props) {
        super(props);
        this.state = {
            numPages: null,
            pageNumber: 1,
            base64Data: null
        }
    }
    // onDocumentLoadSuccess = ({ numPages }) => {
    //     this.setState({ numPages });
    // }

    onDocumentComplete = (pages) => {
        this.setState({ page: 1, pages });
    }

    handlePrevious = () => {
        this.setState({ page: this.state.page - 1 });
    }

    handleNext = () => {
        this.setState({ page: this.state.page + 1 });
    }

    renderPagination = (page, pages) => {
        let previousButton = <li className="previous" onClick={this.handlePrevious}><a href="#"><i className="fa fa-arrow-left"></i> Previous</a></li>;
        if (page === 1) {
            previousButton = <li className="previous disabled"><a href="#"><i className="fa fa-arrow-left"></i> Previous</a></li>;
        }
        let nextButton = <li className="next" onClick={this.handleNext}><a href="#">Next <i className="fa fa-arrow-right"></i></a></li>;
        if (page === pages) {
            nextButton = <li className="next disabled"><a href="#">Next <i className="fa fa-arrow-right"></i></a></li>;
        }
        return (
            <nav>
                <ul className="pager">
                    {previousButton}
                    {nextButton}
                </ul>
            </nav>
        );
    }

    aesList = async () => {
        const startTime = new Date()
        const result = await getAesList()
        const endTime = new Date()
        console.log(result)
        console.log("用时：" + (endTime - startTime))
        const pdfValue = result.data.data
        console.log("pdfValue:" + pdfValue)
        //const conData = converData(pdfValue)
        var pdfDes = window.atob(pdfValue)
        console.log("转换后的pdf：" + pdfDes)
        //用于保存数据  type:application/pdf
        fs.writeFile('test.txt', pdfDes, function(err){
            if(err){
                console.log(err)
                alert(err)
            }else{
                console.log('ok')
                alert("ok")
            }
        })
        alert("123")
        this.setState({ base64Data: pdfDes })

    }

    render() {
        const pdfurl = require('./123.pdf')

        const { base64Data } = this.state
        let pagination = null;
        if (this.state.pages) {
            pagination = this.renderPagination(this.state.page, this.state.pages);
        }

        return (
            <div>Product
            <Button type="primary" onClick={this.aesList}>点击</Button>
                <div>
                    {/* <Document
                        file={base64Data}
                        onLoadSuccess={this.onDocumentLoadSuccess}
                    >
                        <Page pageNumber={pageNumber} />
                    </Document>
                    <p>Page {pageNumber} of {numPages}</p> */}
                    {/* file={`data:application/pdf;base64,${base64Data}`} */}
                    <PDF
                        scale={2}
                        file={pdfurl}
                        onDocumentComplete={this.onDocumentComplete}
                        page={this.state.page}
                    />
                    {pagination}
                </div>
            </div>
        );
    }
}


/**
 * 商品管理 路由
 */
export default Product;